package pers.gxm.memento;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class Gamer {
    private int money;
    private List<String> fruits = new ArrayList<>();
    private Random random = new Random();
    private static String[] fruitsName  = {
       "苹果","葡萄","香蕉","橘子"
    } ;
    public Gamer(int money){
        this.money = money;
    }

    public int getMoney(){
        return money;
    }

    public void bet(){
        int dice = random.nextInt(6)  + 1;
        switch(dice){
            case 1:
                money += 100;
                System.out.println("遭遇事件，金钱增加");
                break;
            case 2:
                money /= 2;
                System.out.println("遭遇事件，金钱减半");
                break;
            case 6:
                String fruit = getFruit();
                System.out.println("遭遇事件，获得水果: " + fruit);
                fruits.add(fruit);
                break;
            default:
                System.out.println("nothing happens");
        }
    }


    public Memento createMemento(){
        Memento memento = new Memento(money);
        Iterator<String> iterator = fruits.iterator();
        while(iterator.hasNext()){
            String next = iterator.next();
            if(next.startsWith("好吃的")){
                memento.addFruit(next);
            }

        }
        return memento;
    }

    public void restoreMemento(Memento memento){
        this.money = memento.money;
        this.fruits = memento.getFruits();
    }

    public String toString(){
        return "[money = " + money + ",fruits = " + fruits + "]";
    }

    private  String getFruit(){
        String prefix = "";
        if(random.nextBoolean()){
            prefix = "好吃的";
        }
        return prefix + fruitsName[random.nextInt(fruitsName.length)];
    }
}
